//
//  WBHomeViewController.swift
//  Weibo
//
//  Created by eju_wangzhenxing on 16/11/4.
//  Copyright © 2016年 qisitianxia. All rights reserved.
//

import UIKit

private let cellId = "cellId"

class WBHomeViewController: WBBaseViewController {

    fileprivate lazy var statusList = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
     
        laodData()
        
        setUpUI()
    }
    
    override func laodData() {
        
        print("加载中...")
        // 模拟延时加载 -> dispatch_after
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
            for i in 0..<15 {
                if self.isPullUp {
                    self.statusList.append("上拉\(i)")
                } else {
                    self.statusList.insert(i.description, at: 0)
                }
            }
            self.talbeView?.reloadData()
            print("加载完成")
            self.refreshControl?.endRefreshing()
            
            self.isPullUp = false
        }
    }
    
    @objc fileprivate func leftClick() {
     
        let vc = WBDemoViewController()
        
        vc.hidesBottomBarWhenPushed = true
        
        navigationController?.pushViewController(vc, animated: true)
    }
}


extension WBHomeViewController {
    
    override func setUpUI() {
        
        //设置导航 按钮
//        navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "好友", style: .plain, target: self, action: #selector(leftClick))
        
        navItem.leftBarButtonItem = UIBarButtonItem.init(title: "好友", titleFontSize: 16, target: self, action: #selector(leftClick))
        
        talbeView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }
}

// MARK: ------------------- 表格数据源 ---------------------
extension WBHomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        
        cell.textLabel?.text = statusList[indexPath.row] as String
        
        return cell
    }
    
}
